#include<REG51.H>

unsigned int duty, free, duty0 = 1,free0 = 1, ratio;

void init()
{
    TMOD = 0X11;
    TH0 = (65536-100)/256;
    TL0 = (65536-100)%256;
    TH1 = (65536-100)/256;
    TL1 = (65536-100)%256;
    EA = 1;//CPU开放中断
    ET0 = 1;//定时器/计数器T0允许溢出中断
    ET1 = 1;//定时器/计数器T1允许溢出中断
    TR0 = 0;//停止计数
    TR1 = 0;//停止计数
    EX0 = 1;//允许外部中断0
    EX1 = 1;//允许外部中断1
    IT0 = 1;//低电平触发
    IT1 = 1;//高电平触发
}

void main()
{
    unsigned char i;
    init();
    while(1)
    {
        ratio = (int)(10.*(duty0)/(duty0+free0));
        if(ratio>8)
        {
            P0 = 0x00;
            if(ratio>9) P2 = 0xfc;
            else P2 = 0xfe;
        }
        else{
            P2 = 0xff;
            P0 = 0xff;
            for(i = 0;i<ratio;i++) P0 <<= 1;
        }
    }
}
//记录高电平时间
void dutytimer() interrupt 1
{
    TH0 = (65536-100)/256;
    TL0 = (65536-100)%256;
    duty++;
}

//记录低电平时间
void freetimer() interrupt 3
{
    TH1 = (65536-100)/256;
    TL1 = (65536-100)%256;
    free ++;
}

void extlnt0() interrupt 0
{
    TR1 = 1;
    TR0 = 0;
    free0 = free;
    free = 0;
}

void extlnt1() interrupt 2
{
    TR0 = 1;
    TR1 = 0;
    duty0 = duty;
    duty = 0;
}

